<template>
  <div>
<el-card>
      <div slot="header">
        <my-breadcrumb>评论管理</my-breadcrumb>
      </div>

      <!-- 表格 -->
      <el-table
        :data="comments">
        <el-table-column label="文章标题" prop="title">
        </el-table-column>
         <el-table-column label="总评论数" prop="total_comment_count">
        </el-table-column>
         <el-table-column label="粉丝评论数" prop="fans_comment_count">
        </el-table-column>
         <el-table-column label="评论状态">
          <!-- 自定义的列模板 利用插槽在评论状态为true的时候显示-->
          <template slot-scope="scope">
            <!-- {{scope.row.comment_status}} -->
            <el-tag v-if="scope.row.comment_status">正常</el-tag>
            <el-tag v-else type="warning">关闭</el-tag>
          </template>
        </el-table-column>
         <el-table-column label="操作">
           <template slot-scope="scope">
            <!-- {{scope.row.comment_status}} 跟前面那个插槽的状态刚好相反-->
            <el-button size="mini" v-if="scope.row.comment_status"
            @click="hSwitch(scope.row)">关闭</el-button>
            <el-button size="mini" v-else type="danger"
            @click="hSwitch(scope.row)">打开</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        background
        layout="prev, pager, next"
        @current-change="hPageChange"
        :total="total_count">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'comment',
  data () {
    return {
      total_count: 0, // 总条数
      page: 1, // 页码
      comments: [] // 评论数据
    }
  },
  created () {
    // 打开页面就触发这个事件
    this.loadComments()
  },
  methods: {
    // 构建参数
    creatParams () {
      const params = {
        page: this.page,
        response_type: 'comment'
      }
      return params
    },
    // 发送请求,拿到评论数据
    async loadComments () {
      const paramsObj = this.creatParams()
      try {
        const res = await this.$axios({
          method: 'GET',
          url: '/mp/v1_0/articles',
          params: paramsObj
        })
        // console.log(res)
        this.comments = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        console.log(err)
      }
    },
    // 点击翻页,重新发送请求
    hPageChange (curPage) {
      this.page = curPage
      this.loadComments()
    },
    // 点击或者关闭评论
    async hSwitch (row) {
      console.log(row)
      try {
        const res = await this.$axios({
          method: 'PUT',
          url: '/mp/v1_0/comments/status',
          params: {
            article_id: row.id.toString()
          },
          data: {
            allow_comment: !row.comment_status
          }
        })
        this.$message.success('操作成功')
        console.log(res)
        // 更新这个数据
        row.comment_status = !row.comment_status
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
